package com.how2java;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.how2java.pojo.Product;

public class TestMybatisTag {

	public static void main(String[] args) throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
				.build(inputStream);
		SqlSession session = sqlSessionFactory.openSession();

		// where 标签 trim标签
		/*
		 * System.out.println("多条件查询"); Map<String,Object> params = new
		 * HashMap<>(); //params.put("name","a"); //params.put("price","10");
		 * List<Product> ps2 = session.selectList("tagWhenOtherwise",params);
		 * for (Product p : ps2) { System.out.println(p); }
		 */
		// set标签 trim标签
		/*
		 * Product p = new Product(); p.setId(6); p.setName("product zztrim");
		 * p.setPrice(99.998f); session.update("tagTrimReplaceSet",p);
		 * 
		 * listAll(session);
		 */

		// foreach标签
		/*
		 * List<Integer> ids = new ArrayList(); ids.add(1); ids.add(3);
		 * ids.add(5);
		 * 
		 * List<Product> ps = session.selectList("tagForeach", ids); for
		 * (Product p : ps) { System.out.println(p); }
		 */

		// bind标签
		Map<String, String> params = new HashMap();
		params.put("name", "x");

		List<Product> ps = session.selectList("tagBind", params);
		for (Product p : ps) {
			System.out.println(p);
		}
		session.commit();
		session.close();

	}

	private static void listAll(SqlSession session) {
		Map<String, Object> params = new HashMap<>();
		// params.put("name","a");
		// params.put("price","10");
		List<Product> ps2 = session.selectList("listProduct", params);
		for (Product p : ps2) {
			System.out.println(p);
		}
	}
}
